#!/usr/bin/env node

var timer;
//1
console.log('hello');

timer = setTimeout(function() {
  clearTimeout(timer); 
  console.log('world');
}, 2000); 


function sleep(milliseconds) {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
}
//2
console.log('hello');

sleep(2000).then(() => {
  console.log('world');
});

//3
console.log('hello');

// 使用 Node.js v15 的 Promise 版本的 setTimeout
setTimeout(() => {
  console.log('world');
}, 2000);

// 或者使用 Promise 封装
function sleep(milliseconds) {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
}

console.log('hello');
sleep(2000).then(() => {
  console.log('world');
});